﻿using RequirementAndBug.Common.Entities.UserModels;
using RequirementAndBug.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RequirementAndBug.BLL
{
    public class UserService : BaseService
    {
        public int CheckUserLogin(string userCode, string password,out UserInfo userInfo )
        {
            userInfo = null; 
            IQueryable<REQUser> list = LoadEntities<REQUser>(x => x.UserCode == userCode && x.Password == password && x.Status==1);
            if (list.Count()> 0)
            {
                userInfo = new UserInfo();

                REQUser user = list.FirstOrDefault();
                userInfo.NickName = user.NickName;
                userInfo.UserCode = user.UserCode;
                userInfo.UserID = user.UserID;
                userInfo.RoleID = user.Role.RoleID;
                return 1;
            }
            return 0;        
        }

        public List<UserInfo> GetNormalUserList()
        {
            IQueryable<REQUser> list = LoadEntities<REQUser>(x => x.UserType == 0 && x.Status == 1);

            List<UserInfo> userList = new List<UserInfo>();

            //foreach (var u in list)
            //{
            //    UserInfo info = new UserInfo();
            //    info.NickName = u.NickName;
            //    info.UserCode = u.UserCode;
            //    info.UserID = u.UserID;
            //    userList.Add(info);
            //}
            //return userList;        

            var dataList = list.Select(entity => new UserInfo { NickName=entity.NickName, UserCode=entity.UserCode,UserID=entity.UserID });
            userList = dataList.ToList();
            return userList;
        }
    }
}
